package com.chen.back.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.back.entity.Role;
import com.chen.back.entity.RolePermission;
import com.chen.back.entity.UserRole;
import com.chen.back.response.Result;
import com.chen.back.service.RolePermissionService;
import com.chen.back.service.RoleService;
import com.chen.back.service.UserRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chen
 * @since 2022-02-24
 */
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController{

@Autowired
private RoleService roleService;
@Autowired
private RolePermissionService rolePermissionService;
@Autowired
private UserRoleService userRoleService;

    @GetMapping("/list")
    public String list(String name) {

        Page<Role> pageData = roleService.page(getPage(),
                new QueryWrapper<Role>()
                        .like(StrUtil.isNotBlank(name), "role_name", name)
        );

        return JSON.toJSONString(new Result().setCode(200).setData(pageData));

    }
    @GetMapping("/list1")
    public String list1() {

        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.notIn("role_id",4);
        List<Role> list= roleService.list(wrapper);


        return JSON.toJSONString(new Result().setCode(200).setData(list));

    }


    @GetMapping("/info/{id}")
    public String  info(@PathVariable("id") Integer id) {
        Role role = roleService.getById(id);

        // 获取角色相关联的菜单id
        List<Integer> permissionIds = roleService.getAllPermissionIdByRoleId(id);
        role.setPermissionIds(permissionIds);

        return JSON.toJSONString(new Result().setCode(200).setData(role));

    }

    @PostMapping("/save")
    public String save(@RequestBody Role role) {

        roleService.save(role);
        return JSON.toJSONString(new Result().setCode(200).setData("添加成功"));

    }

    @PostMapping("/delete")
    @Transactional
    public String info(@RequestBody Integer[] ids) {

        roleService.removeByIds(Arrays.asList(ids));

        // 删除中间表
        userRoleService.remove(new QueryWrapper<UserRole>().in("role_id", ids));
        rolePermissionService.remove(new QueryWrapper<RolePermission>().in("role_id", ids));



        return JSON.toJSONString(new Result().setCode(200).setData("添加成功"));

    }

    @PostMapping("/update")
    public String update( @RequestBody Role role) {


        roleService.updateById(role);

        // 更新缓存

        return JSON.toJSONString(new Result().setCode(200).setData(role));

    }


}
